import {Overlay} from 'teaset';
import React from 'react';
const modalViews = [];
const ModalViewUtil = {
  showFromBottom(
    view,
    containerStyle?,
    onDisappearCompleted? = null,
    onAppearCompleted? = null,
  ) {
    return ModalViewUtil.showFromPosition(
      view,
      'bottom',
      false,
      containerStyle,
      onDisappearCompleted,
      onAppearCompleted,
    );
  },
  showFromPosition(
    view,
    position? = 'bottom',
    modal? = false,
    containerStyle?,
    onDisappearCompleted? = null,
    onAppearCompleted? = null,
    props? = {},
  ) {
    ModalViewUtil.overlayView = (
      <Overlay.PullView
        side={position}
        modal={modal}
        rootTransform={'none'}
        onDisappearCompleted={() => {
          onDisappearCompleted && onDisappearCompleted();
          Overlay.hide(ModalViewUtil.key);
        }}
        {...props}
        containerStyle={containerStyle}
        onAppearCompleted={onAppearCompleted}
        ref={v => {
          if (v) {
            ModalViewUtil.overlayPullView = v;
            modalViews.push({
              key: ModalViewUtil.key,
              element: ModalViewUtil.overlayPullView,
            });
          }
        }}>
        {view}
      </Overlay.PullView>
    );
    ModalViewUtil.key = Overlay.show(ModalViewUtil.overlayView);
    return ModalViewUtil.key;
  },
  showFromTop(
    view,
    containerStyle?,
    onDisappearCompleted? = null,
    onAppearCompleted? = null,
  ) {
    return ModalViewUtil.showFromPosition(
      view,
      'top',
      false,
      containerStyle,
      onDisappearCompleted,
      onAppearCompleted,
    );
  },
  showFromTopWithOverlayProps(
    view?,
    containerStyle?,
    props?,
    onDisappearCompleted? = null,
    onAppearCompleted? = null,
  ) {
    return ModalViewUtil.showFromPosition(
      view,
      'top',
      false,
      containerStyle,
      onDisappearCompleted,
      onAppearCompleted,
      props,
    );
  },
  dismissBottomView() {
    if (modalViews.length > 0) {
      let key = modalViews.pop();
      key.element.close();
    }
  },
  dismissView() {
    if (modalViews.length > 0) {
      let key = modalViews.pop();
      key.element.close();
      Overlay.hide(key.key);
    }
  },
  showFromCenterWithOverlayProps(
    view,
    modal?,
    props?,
    onDisappearCompleted? = null,
    onAppearCompleted? = null,
  ) {
    return ModalViewUtil.showFromCenter(
      view,
      modal,
      {},
      onDisappearCompleted,
      onAppearCompleted,
      props,
    );
  },
  showFromCenter(
    view,
    modal? = false,
    containerStyle?,
    onDisappearCompleted? = null,
    onAppearCompleted? = null,
    props?,
  ) {
    let overlayView = (
      <Overlay.PopView
        style={{alignItems: 'center', justifyContent: 'center'}}
        type={'zoomOut'}
        modal={modal}
        onDisappearCompleted={() => {
          onDisappearCompleted && onDisappearCompleted();
          Overlay.hide(ModalViewUtil.key);
        }}
        {...props}
        containerStyle={containerStyle}
        onAppearCompleted={onAppearCompleted}
        ref={v => {
          if (v) {
            ModalViewUtil.overlayPullView = v;
            modalViews.push({
              key: ModalViewUtil.key,
              element: ModalViewUtil.overlayPullView,
            });
          }
        }}>
        {view}
      </Overlay.PopView>
    );
    ModalViewUtil.key = Overlay.show(overlayView);
    return ModalViewUtil.key;
  },
};

export default ModalViewUtil;
